package middleware

//跨域验证，参考网站https://www.ruanyifeng.com/blog/2016/04/cors.html
import (
	"github.com/gin-contrib/cors" //gin官方跨域包，基本复制
	"github.com/gin-gonic/gin"
	"time"
)

// Cors 跨域中间件，没用cookie（其实也不能跨域）
func Cors() gin.HandlerFunc {
	return cors.New(
		cors.Config{
			//AllowAllOrigins:  true,
			AllowOrigins:     []string{"*"},                                       // 等同于允许所有域名 #AllowAllOrigins:  true
			AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, //增删改查都允许
			AllowHeaders:     []string{"*", "Authorization"},
			ExposeHeaders:    []string{"Content-Length", "text/plain", "Authorization", "Content-Type"},
			AllowCredentials: true,
			MaxAge:           12 * time.Hour, //预请求的持续时间
		},
	)
}
